<?php

namespace App\Http\Controllers\UserApi;

use App\Http\Controllers\Controller;
use App\Models\GoodsIntegralUsable;
use App\Models\User;
use App\Services\Api\GoodsServices;
use Illuminate\Http\Request;
use App\Services\Api\UserTrainServices;
use App\Models\UserTrain;
use App\Models\UserSubscribe;
use App\Services\Api\UserRankExpServices;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\DB;



class UserTrainContoller extends BaseController
{
    public function user_train(Request $request,UserTrainServices $trainServices,UserTrain $userTrain){
        $member_id = auth('api') -> user()->id;
        $query = $userTrain->where('user_id',$member_id);
        if(!empty($request->input('where_date'))){
            $start=date('Y-m-01 00:00:00',strtotime($request->input('where_date')));//获取指定月份的第一天
            $end=date('Y-m-t 23:59:59',strtotime($request->input('where_date'))); //获取指定月份的最后一天
            //dd($start,$end);
            //$query->whereDate('created_at',$request->input('where_date'));
            $query->whereBetween('created_at',[$start,$end]);
        }
        $data = $query
            ->with([
                'class_base'=>function($q){
                    $q->select(['id','class_name','class_cove']);
                },
                'teacher'=>function($q){
                    $q->select(['id','teacher_name']);
                },
                'classroom'=>function($q){
                    $q->select(['id','numbering']);
                },
                'store'=>function($q){
                    $q->select(['id','store_name']);
                },
                'class_base_info'=>function($q){
                    $q->select(['id','class_time']);
                },
            ])->paginate(10);
        return $this->success($data);
    }

    /**
     * 前端签到逻辑
     * @param Request $request
     * @param UserTrain $userTrain
     * @param UserRankExpServices $expServices
     * @return \Illuminate\Http\JsonResponse
     */
    public function sign_train(){
        date_default_timezone_set('PRC');

        $goodsServices = (new GoodsServices());
        $userTrain = (new UserTrain());
        $expServices = (new UserRankExpServices());

        DB::beginTransaction();
        try{
            $subscribe_id = request()->input('subscribe_id');

            $subscribe = (new UserSubscribe())->WhereID($subscribe_id)
                ->with([
                    'class_base_info'=>function($q){
                        $q->with(['quarterly']);
                    },
                ])
                ->first();

            $goodsServices->sign_train_bool($subscribe);

            //添加商品积分
            $goodsServices->add_goods_integral(auth('api') -> user()->id,$subscribe->id);

            //簽到記錄
            $res = $userTrain->insert([
                'user_id'=>auth('api') -> user()->id,
                'class_base_id'=>$subscribe->class_base_id,
                'teacher_id'=>$subscribe->teacher_id,
                'store_id'=>$subscribe->store_id,
                'subscribe_id'=>$subscribe->id,
                'class_classroom_id'=>$subscribe->class_classroom_id,
                'class_base_info_id'=>$subscribe->class_base_info_id,
                'train_time'=>(strtotime($subscribe->end_time) - strtotime($subscribe->class_time)) / 60,
                'at_integral'=>$subscribe->use_integral,
                'created_at'=>date("Y-m-d H:i:s"),
            ]);

            $subscribe->subscribe_status = 5;

            $subscribe->save();

            User::where('id',auth('api') -> user()->id)->increment('total_train_time',(strtotime($subscribe->end_time) - strtotime($subscribe->class_time)) / 60);

            $expData = [
                'exp_value'=>$subscribe->use_integral,
                'user_id'=>auth('api') -> user()->id,
                'type'=>1,
                'remarks'=>'签到积分',
                'created_at'=>date("Y-m-d H:i:s")
            ];

            $expServices->add_user_rank_exp($expData);

            $class_name = $subscribe->class_name??'';

            $reason = "{$class_name}-簽到成功";

            GoodsServices::goods_record($subscribe->use_integral, 1, $reason);

            DB::commit();

        }catch (\Exception $e){

            DB::rollBack();

            return $this->fail($e->getMessage());
        }


        return $this->success();
    }
}
